package com.itcast.manager2.B_contorller;

import com.itcast.manager2.C_service.StudentService;
import com.itcast.manager2.D_dao.StudentDao;
import com.itcast.manager2.E_domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentContorller {
    private Scanner sc = new Scanner(System.in);
    private StudentService ss = new StudentService();

    public void start() {

        Scanner sc = new Scanner(System.in);
        // ctrl + Alt + T
        while (true) {
            // 界面展示
            System.out.println("------欢迎使用黑马学生系统------");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查看学生");
            System.out.println("5. 退出");
            // 接受用户的选择
            int choose = sc.nextInt();
            // 匹配用户的选择
            switch (choose) {
                case 1:
                    // 将用户录入的学生信息，存入集合中
                    addStudent();  // 集合中已经有一个学生
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    // 将集合中的学生对象，进行展示
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("退出");
                    return; // 结束main方法，程序结束
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }

    private void updateStudent() {
        System.out.println("请输入要修改的学生学号");
        String id = sc.next();
        if (getturefalse(id)==true){
            System.out.println("请输入修改后的姓名");
            String name = sc.next();
            System.out.println("请输入修改后的年龄");
            int  age = sc.nextInt();
            System.out.println("请输入修改后的生日");
            String birthday = sc.next();
            Student stu = new Student(id,name,age,birthday);
            ss.updateStdent(stu);
        }else {
            System.out.println("该学号不存在");
            return;
        }
    }


    private void deleteStudent() {
        System.out.println("请输入要删除学生学号：");
        String id = sc.next();
        if (getturefalse(id) == true) {
            System.out.println("学号已删除");
            ss.deleteStudent(id);
            return;
        }
            System.out.println("没有该学生的信息");

    }

    ;


    private void findAllStudent() {
       ArrayList<Student> list = ss.findAllStudent();
         if (list.size()==0){
             System.out.println("管理系统里面目前还没有信息");
             return;
         }
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"--"+stu.getName()+"--"+stu.getAge()+"--"+stu.getBirthday());
        }
    }

    // 添加学生方法
    public void addStudent() {
        System.out.println("请输入学生学号：");
        String id = sc.next();
        if (getturefalse(id) == true) {
            System.out.println("学号已存在");
            return;
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        // 将上面录入的信息，封装到学生对象中，再把对象存入集合
        Student s = new Student(id, name, age, birthday);
        // 对象存入集合
        ss.addStudent(s);
        System.out.println("添加成功");
    }

    public boolean getturefalse(String id) {
        while (true) {
            boolean flag = ss.isExists(id);
            if (false == flag) {
                return false;
            } else {
                return true;
            }
        }
    }


}
